import sys,math,bisect
from functools import lru_cache
from random import randint
inf = float('inf')
mod = (10**9)+7
"========================================"
def nCr(n, r):
return (fact(n) / (fact(r)
* fact(n - r)))
def fact(n):
res = 1
for i in range(2, n+1):
res = res * i
return res
def lcm(a,b):
return int((a/math.gcd(a,b))*b)
def gcd(a,b):
return int(math.gcd(a,b))
def tobinary(n):
return bin(n)[2:]
def binarySearch(a,x):
i = bisect.bisect_left(a,x)
if i!=len(a) and a[i]==x:
return i
else:
return -1
def lowerBound(a, x):
i = bisect.bisect_left(a, x)
if i:
return (i-1)
else:
return -1
def upperBound(a,x):
i = bisect.bisect_right(a,x)
if i!= len(a)+1 and a[i-1]==x:
return (i-1)
else:
return -1
def primesInRange(n):
ans = []
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
for p in range(2, n+1):
if prime[p]:
ans.append(p)
return ans
def primeFactors(n):
factors = set()
while n % 2 == 0:
factors.add(2)
n = n // 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
factors.add(i)
n = n // i
if n > 2:
factors.add(n)
return factors
def isPrime(n,k=8):
if (n <2):
return True
for i in range(0,k):
a = randint(1,n-1)
if(pow(a,n-1,n)!=1):
return False
return True
def find_closest(arr,target):
arr.sort()
min_diff = 1e19
low = 0
high = len(arr)-1
closest_num = None
if len(arr)==0:
return None
if len(arr)==1:
return arr[0]
while low <= high:
mid = (low+high)//2
min_diff_left = 1e19
min_diff_right = 1e19
if mid +1 < len(arr):
min_diff_right = abs(arr[mid+1]-target)
if mid -1 >= 0:
min_diff_left = abs(arr[mid-1]-target)
if min_diff_left < min_diff:
min_diff = min_diff_left
closest_num = arr[mid-1]
if min_diff_right < min_diff:
min_diff = min_diff_right
closest_num = arr[mid+1]
if arr[mid]<target:
low = mid+1
elif arr[mid]>target:
high = mid-1
else:
return arr[mid]
return closest_num
"========================================="
from collections import deque,defaultdict,Counter
from heapq import heappush, heappop,heapify
import string
for _ in range(1):
n,m = map(int,input().split())
cost = list(map(int,input().split()))
graph = defaultdict(list)
for _ in range(m):
s,u = map(int,input().split())
graph[s].append(u)
graph[u].append(s)
groups = []
visited = set()
for i in range(1,n+1):
if i not in visited:
curr = []
Q = deque([i])
while Q:
s = Q.popleft()
curr.append(s)
visited.add(s)
for nei in graph[s]:
if nei not in visited:
Q.append(nei)
groups.append(curr)
ans = 0
for group in groups:
curr = 1e19
for i in group:
curr = min(curr,cost[i-1])
ans += curr
print(ans)
#include <iostream>
#include <bits/stdc++.h>
#include <numeric>
#include <algorithm>
typedef long long ll;
using namespace std;
class Person {
public:
ll m;
vector<Person*> friends;
int f = 0;
bool visited = false;
int Spread_Rumor(int min) {
visited = true;
//cout << m << endl;
//cout << visited << endl;
if (m < min) {
min = m;
}
for (int i = 0; i < f; i++) {
if (friends[i]->visited == false) {
//cout << "Got here" << endl;
min = friends[i]->Spread_Rumor(min);
}
}
return min;
}
};
int main() {
int n, m;
cin >> n >> m;
vector<Person*> comm(n);
ll p;
for (int i = 0; i < n; i++) {
cin >> p;
Person* per = new Person();
per->m = p;
comm[i] = per;
}
int x, y;
for (int i = 0; i < m; i++) {
cin >> x >> y;
comm[x-1]->friends.push_back(comm[y-1]);
comm[x-1]->f++;
comm[y-1]->friends.push_back(comm[x-1]);
comm[y-1]->f++;
}
ll ans = 0;
ans += comm[0]->Spread_Rumor(comm[0]->m);
//cout << "Spreading rumor for first element, result is " << max << endl;
int c;
for (int i = 1; i < n; i++) {
//cout << comm[i].f << endl;
if (comm[i]->visited == false) {
//cout << comm[i].visited << endl;
//cout << "New Rumor:" << endl;
c = comm[i]->Spread_Rumor(comm[i]->m);
//cout << "Rumor for " << i+1 << " is " << c << endl;
ans += c;
}
}
cout << ans;
return 0;
}
1534B - Histogram Ugliness | 1611B - Team Composition Programmers and Mathematicians |
110A - Nearly Lucky Number | 1220B - Multiplication Table |
1644A - Doors and Keys | 1644B - Anti-Fibonacci Permutation |
1610A - Anti Light's Cell Guessing | 349B - Color the Fence |
144A - Arrival of the General | 1106A - Lunar New Year and Cross Counting |
58A - Chat room | 230A - Dragons |
200B - Drinks | 13A - Numbers |
129A - Cookies | 1367B - Even Array |
136A - Presents | 1450A - Avoid Trygub |
327A - Flipping Game | 411A - Password Check |
1520C - Not Adjacent Matrix | 1538B - Friends and Candies |
580A - Kefa and First Steps | 1038B - Non-Coprime Partition |
43A - Football | 50A - Domino piling |
479A - Expression | 1480A - Yet Another String Game |
1216C - White Sheet | 1648A - Weird Sum |